﻿/*  CamHelper (See http://camhelper.codeplex.com/ or https://bitbucket.org/AndiKS/camhelper for more Info)
    Copyright (C) 2011 Andreas Wimmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using CamHelper.Engine;
using CamHelper.Facade;
using Ninject;

namespace CamHelper.Command
{
    public static class CommandFactory
    {

        #region Commands

        private static Dictionary<string, IOfflineCommand> _offlineCommands = FillOfflineCommands();

        private static Dictionary<string, IOfflineCommand> FillOfflineCommands()
        {
            var dic = new Dictionary<string, IOfflineCommand>();
            dic.Add("test", new TestCommand());
            dic.Add("testparser", new ParserTestCommand());
            return dic;
        }

        private static Dictionary<string, IOnlineCommand> _onlineCommands = FillOnlineCommands();

        private static Dictionary<string, IOnlineCommand> FillOnlineCommands()
        {
            var dic = new Dictionary<string, IOnlineCommand>();
            dic.Add("opendirectory", new OpenDirectoryCommand());
            return dic;
        }

        #endregion

        #region CommandExists

        public static bool CommandExists(string cmd)
        {
            return _offlineCommands.ContainsKey(cmd) || _onlineCommands.ContainsKey(cmd);
        }

        #endregion

        #region RunCommand

        public static void RunCommand(string cmd)
        {
            if (_offlineCommands.ContainsKey(cmd))
            {
                IOfflineCommand c = _offlineCommands[cmd];
                c.Run();
            }
            else if (_onlineCommands.ContainsKey(cmd))
            {
                var kernel = new StandardKernel(new RealModul());
                var watcher = kernel.Get<ITopSolidWatcher>();

                ITopSolidApplication app = null;
                ITopSolidDocument doc = null;

                if (!watcher.IsRunning())
                {
                    Console.WriteLine("TopSolid is not running!");
                    return;
                }

                try
                {
                    app = kernel.Get<ITopSolidApplication>();
                    doc = app.GetCurrentDocument();

                    IOnlineCommand c = _onlineCommands[cmd];
                    c.Run(app, doc);
                }
                finally
                {
                    if (doc != null)
                    {
                        doc.Dispose();
                        doc = null;
                    }
                    if (app != null)
                    {
                        app.Dispose();
                        app = null;
                    }
                }
            }
            else
            {
                throw new CommandNotFoundException(cmd);
            }
        }

        #endregion

        #region ShowCommands

        public static void ShowCommands()
        {
            Console.WriteLine("Please use one of this Parameter to start a Command:");
            List<String> list = new List<string>();
            list.AddRange(_offlineCommands.Keys);
            list.AddRange(_onlineCommands.Keys);

            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();
            Console.WriteLine("Press Enter to Exit");
            Console.ReadLine();
        }

        #endregion
    }
}
